package com.ydgk.jishuzhan.controller;

import com.ydgk.jishuzhan.beans.*;
import com.ydgk.jishuzhan.service.*;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.sql.Date;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;


@RequestMapping("/blog")
@Controller
public class BlogController {

    @Autowired
    private BlogService blogService;

    @Autowired
    private UserService userService;

    @Autowired
    private QuestionService questionService;

    @Autowired
    private TalkService talkService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private ScoreService scoreService;

    @RequestMapping("/toBlog")
    public String toBlog(Model model) {
        List<Blog> blogs = blogService.findBlogs();
        model.addAttribute("blogs",blogs);
        return "blog";

    }

    @RequestMapping("/toAdd")
    public String toAdd() {
        return "blog-add";
    }


    @RequestMapping("/doAdd")
    public String doAdd(Blog blog, Model model) {
        Date date = new Date(System.currentTimeMillis());
        blog.setSendTime(date);
        blogService.add(blog);
        scoreService.scoreAdd(new Score(null,"积分增加","编写博客",5,date,blog.getUserId()));
        List<Blog> blogs=blogService.findBlogs();
        List<Question> questions=questionService.findQuestions();
        List<Talk> talks=talkService.findTalks();
        model.addAttribute("blogs",blogs);
        model.addAttribute("questions",questions);
        model.addAttribute("talks",talks);
        return "user-home-for-me";

    }

    @RequestMapping("/toBlog-detail")
    public String toBlogDetail(Integer id,Model model){
        Blog blog = blogService.findBlog(id);
        blog.getBlogInfo().setUser(userService.findUserById(blog.getUserId()));
        model.addAttribute("blog",blog);
        List<Blog> blogs = blogService.findBlogs();
        model.addAttribute("blogs",blogs);
        List<Comment> comments=commentService.findCommentsByBId(id);
        model.addAttribute("comments",comments);
        int size = comments.size();
        model.addAttribute("commentSize",size);

        int goodTotal= blogService.findGoods(id);
        model.addAttribute("goodTotal",goodTotal);

        int good=blogService.findGood(blog.getUserId(),id);
        model.addAttribute("good",good);

        int collectionTotal=blogService.findCollections(id);
        int collection=blogService.findCollection(blog.getUserId(),id);
        model.addAttribute("collectionTotal",collectionTotal);
        model.addAttribute("collection",collection);

        blogs.sort(new Comparator<Blog>() {
            public int compare(Blog arg0, Blog arg1) {
                return arg1.getId() - arg0.getId();
            }
        });

        if (id>=blogs.get(0).getId()){
            Blog blog1=blogService.findBlog(id-1);
            model.addAttribute("frontBlog",blog1.getTitle());
            model.addAttribute("behindBlog",null);
        }else if(id==1){
            Blog blog2=blogService.findBlog(id+1);
            model.addAttribute("frontBlog",null);
            model.addAttribute("behindBlog",blog2);
        }else {
            Blog blog1=blogService.findBlog(id-1);
            Blog blog2=blogService.findBlog(id+1);
            model.addAttribute("frontBlog",blog1);
            model.addAttribute("behindBlog",blog2);
        }



        return "blog-detail";
    }

    @ResponseBody
    @RequestMapping("/goodAdd")
    public AjaxResult goodAdd(@Param("userId")Integer userId,@Param("blogId") Integer blogId){
        AjaxResult ajaxResult = new AjaxResult();
        int total=blogService.findGood(userId,blogId);
        if (total==0){
            try{
                blogService.goodAdd(userId,blogId);
                ajaxResult.setSuccess(true);
            }catch (Exception e){
                ajaxResult.setSuccess(false);
                e.printStackTrace();
            }
        }else {
            ajaxResult.setSuccess(false);
        }

        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping("/collect")
    public AjaxResult collect(@Param("userId")Integer userId,@Param("blogId") Integer blogId){
        AjaxResult ajaxResult = new AjaxResult();
        int total=blogService.findCollection(userId,blogId);
        if (total==0){
            try{
                blogService.collectionAdd(userId,blogId);
                ajaxResult.setSuccess(true);
            }catch (Exception e){
                ajaxResult.setSuccess(false);
                e.printStackTrace();
            }
        }else {
            ajaxResult.setSuccess(false);
        }

        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping("/deleteBCollection")
    public AjaxResult deleteById(@RequestParam("id") Integer id){
        AjaxResult ajaxResult = new AjaxResult();
        try{
            blogService.deleteBCollection(id);
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            ajaxResult.setSuccess(false);
            e.printStackTrace();
        }

        return ajaxResult;
    }
}